import org.apache.poi.hssf.usermodel.HSSFWorkbook
import ru.niir.goodfil.db.Good
import ru.niir.goodfil.db.GoodfilFactory

String FILENAME = "c:\\1.xls";
int LIST_NUMBER = 0;
int CODE_COLUMN = 0;
int WEIGHT_COLUMN = 1;
int VOLUME_COLUMN = 2;


gf = GoodfilFactory.instance;

wb = new HSSFWorkbook(new FileInputStream(FILENAME));
sheet = wb.getSheetAt(LIST_NUMBER);

List<Good> allGoods = gf.getGoods(0, Integer.MAX_VALUE, null);

for (def i = 1; i < sheet.lastRowNum; i++) {
  row = sheet.getRow(i);

  try {
    String code = row.getCell(CODE_COLUMN).stringCellValue.trim();
    if (code == null || code.equals("")) continue;

    Float volume = null;
    Float weight = null;
    try {

      try {
        volume = row.getCell(VOLUME_COLUMN).numericCellValue;
      } catch (Exception e) {
        println "> ${row.getCell(VOLUME_COLUMN).stringCellValue}"
        e.printStackTrace();
        System.exit 0;
      }

      try {
        weight = row.getCell(WEIGHT_COLUMN).numericCellValue;
      } catch (Exception e) {
        println "> ${row.getCell(WEIGHT_COLUMN).stringCellValue}"
        e.printStackTrace();
        System.exit 0;
      }

      boolean needToUpdate = false;
      Good good = null;

      for (Good g : allGoods) {
        if (g.getCode().equals(code.trim())) {
          good = g;
        }
      }

      if (good != null && (weight != null || volume != null)) {
        if (weight != null) {
          if (!good.getWeight().equals(weight)) {
            good.setWeight(weight);
            needToUpdate = true;
          }
        }
        if (volume != null) {
          if (!good.getVolume().equals(volume)) {
            good.setVolume(volume);
            needToUpdate = true;
          }
        }

        if (needToUpdate) {
          gf.updateGood(good);
          println "+ ${good.code}";
        }
      } else {
        println "- ${code}";
      }
    } catch (Exception e) {
      println "? ${code}, ${weight}, ${volume}"
      e.printStackTrace();
      System.exit(0);
    }
  } catch (Exception e) {
    println "line ${i}";
    e.printStackTrace();
    System.exit(0);
  }
}